home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 039a / cdbw.zip / SADDRESS.C < prev    next >
Text File  |  1991-05-15  |  10KB  |  321 lines

  1. /*
  2.  *  SADDRESS.C
  3.  *
  4.  *  This module contains functions associated with the address dialog box
  5.  *  in SAMPLE.EXE.
  6.  *
  7.  *  Copyright (C) 1991 by Daytris.  All rights reserved.
  8.  */
  9.  
  10. #include <windows.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #ifndef ZORTECH
  14. #include <memory.h>
  15. #endif
  16. #include "dbmgr.h"
  17. #include "sampledb.h"
  18. #include "sample.h"
  19.  
  20.  
  21. /************************************************
  22.  * Local data
  23.  ************************************************/
  24.  
  25. static ADDRESS address;
  26.  
  27. static enum 
  28.     {
  29.     MODE_ADD,
  30.     MODE_UPDATE,
  31.     MODE_DELETE
  32.     } eMode;
  33.  
  34.  
  35. /************************************************
  36.  * Function Declarations
  37.  ************************************************/
  38.  
  39. BOOL AddAddressDlg( HWND hWnd);
  40. BOOL UpdateAddressDlg( HWND hWnd);
  41. BOOL DeleteAddressDlg( HWND hWnd);
  42. static BOOL GetSelectedAddress( HWND hWnd, ADDRESS *pAddress, short *pIndex);
  43. BOOL FAR PASCAL AddressProc( HWND hDlg, unsigned iMessage, WORD wParam,
  44.     LONG lParam);
  45. static void SetAddressFields( HWND hDlg);
  46. static BOOL GetAddressFields( HWND hDlg);
  47.  
  48.  
  49. /***************************************************************************
  50.  * Function : AddAddressDlg
  51.  *
  52.  * Purpose  : This function drives the Add Address dialog box.  The
  53.  *            database is not updated, only the address listbox.
  54.  *
  55.  * Returns  : TRUE  - address added
  56.  *            FALSE - address not added
  57.  ***************************************************************************/
  58. BOOL AddAddressDlg( HWND hWnd)
  59.     {
  60.     short nStatus;
  61.     FARPROC lpfnAddressProc;
  62.  
  63.     eMode = MODE_ADD;
  64.  
  65.     /* Initialize the address structure */
  66.     memset( &address, 0, sizeof( ADDRESS));
  67.  
  68.     /* Create an instance and open the Address window */
  69.     lpfnAddressProc = MakeProcInstance( AddressProc, hInst);
  70.     nStatus = DialogBox( hInst, "address", hWnd, lpfnAddressProc);
  71.     FreeProcInstance( lpfnAddressProc);
  72.  
  73.     /* User selected OK */
  74.     if( nStatus == IDOK)
  75.         {
  76.         /* Add the address to listbox in the Client dialog */
  77.         if( ! AddToAddressListBox( hWnd, &address))
  78.             return FALSE;
  79.         }
  80.  
  81.     return TRUE;
  82.     }
  83.  
  84.  
  85. /***************************************************************************
  86.  * Function : UpdateAddressDlg
  87.  *
  88.  * Purpose  : This function drives the Update Address dialog box.  The
  89.  *            database is not updated, only the address listbox.
  90.  *
  91.  * Returns  : TRUE  - address updated
  92.  *            FALSE - address not updated
  93.  ***************************************************************************/
  94. BOOL UpdateAddressDlg( HWND hWnd)
  95.     {
  96.     short nStatus, nIndex;
  97.     FARPROC lpfnAddressProc;
  98.  
  99.     eMode = MODE_UPDATE;
  100.  
  101.     /* Retrieve the selected address */
  102.     if( ! GetSelectedAddress( hWnd, &address, &nIndex))
  103.         {
  104.         MessageBeep( 0);
  105.         return FALSE;
  106.         }
  107.  
  108.     /* Create an instance and open the Address window */
  109.     lpfnAddressProc = MakeProcInstance( AddressProc, hInst);
  110.     nStatus = DialogBox( hInst, "address", hWnd, lpfnAddressProc);
  111.     FreeProcInstance( lpfnAddressProc);
  112.  
  113.     /* User selected OK */
  114.     if( nStatus == IDOK)
  115.         {
  116.         /* Update the address */
  117.         if( ! DeleteFromAddressListBox( hWnd, nIndex))
  118.             return FALSE;
  119.         if( ! AddToAddressListBox( hWnd, &address))
  120.             return FALSE;
  121.         }
  122.  
  123.     return TRUE;
  124.     }
  125.  
  126.  
  127. /***************************************************************************
  128.  * Function : DeleteAddressDlg
  129.  *
  130.  * Purpose  : This functions queries about deleting the selected address
  131.  *            record.  The database is not updated, only the address
  132.  *            listbox.
  133.  *
  134.  * Returns  : TRUE  - address deleted
  135.  *            FALSE - address not deleted
  136.  ***************************************************************************/
  137. BOOL DeleteAddressDlg( HWND hWnd)
  138.     {
  139.     short nStatus, nIndex;
  140.  
  141.     /* Retrieve the selected address */
  142.     if( ! GetSelectedAddress( hWnd, &address, &nIndex))
  143.         {
  144.         MessageBeep( 0);
  145.         return FALSE;
  146.         }
  147.  
  148.     /* Ask user if they're sure */
  149.     nStatus = MessageBox( hWnd, "Are you sure?", "Delete Address",
  150.         MB_ICONQUESTION | MB_YESNO);
  151.  
  152.     /* User selected YES */
  153.     if( nStatus == IDYES)
  154.         {
  155.         /* Remove the address from the listbox */
  156.         if( ! DeleteFromAddressListBox( hWnd, nIndex))
  157.             return FALSE;
  158.         }
  159.  
  160.     return TRUE;
  161.     }
  162.  
  163.  
  164. /***************************************************************************
  165.  * Function : GetSelectedAddress
  166.  *
  167.  * Purpose  : This function retrieves the selected address record from
  168.  *            memory.  The handle to the address is stored in the listbox
  169.  *            string.  The selected index is also returned.
  170.  *
  171.  * Returns  : TRUE  - address retrieved
  172.  *            FALSE - error
  173.  ***************************************************************************/
  174. static BOOL GetSelectedAddress( HWND hWnd, ADDRESS *pAddress, short *pIndex)
  175.     {
  176.     HANDLE hAddress;
  177.     ADDRESS FAR *lpAddress;
  178.  
  179.     /* Get the listbox selection */
  180.     if( (*pIndex = (short)SendMessage( GetDlgItem( hWnd, IDC_ADDR_LISTBOX),
  181.         LB_GETCURSEL, 0, 0L)) == LB_ERR)
  182.         {
  183.         return FALSE;
  184.         }
  185.     hAddress = GetAddressHandle( hWnd, *pIndex);
  186.  
  187.     /* Copy the Address info */
  188.     if( (lpAddress = (ADDRESS FAR *)GlobalLock( hAddress)) == NULL)
  189.         {
  190.         MessageBox( hWnd, "Memory: GlobalLock", "Fatal Error",
  191.             MB_ICONEXCLAMATION | MB_OK);
  192.         return FALSE;
  193.         }
  194.     _fmemcpy( pAddress, lpAddress, sizeof( ADDRESS));
  195.     GlobalUnlock( hAddress);
  196.  
  197.     return TRUE;
  198.     }
  199.  
  200.  
  201. /***************************************************************************
  202.  * Function : AddressProc
  203.  *
  204.  * Purpose  : This function is the window procedure for 'add' and 'update'
  205.  *            address.
  206.  *
  207.  * Returns  : TRUE  - message processed
  208.  *            FALSE - message not processed
  209.  ***************************************************************************/
  210. BOOL FAR PASCAL AddressProc( HWND hDlg, unsigned iMessage, WORD wParam,
  211.     LONG lParam)
  212.     {
  213.     switch( iMessage)
  214.         {
  215.         case WM_INITDIALOG:
  216.             if( eMode == MODE_ADD)
  217.                 SetWindowText( hDlg, "Add Address");
  218.             else
  219.                 SetWindowText( hDlg, "Update Address");
  220.  
  221.             SetAddressFields( hDlg);
  222.  
  223.             /* Set focus to Street field */
  224.             SetFocus( GetDlgItem( hDlg, IDC_EDIT_STREET));
  225.             return TRUE;
  226.  
  227.         case WM_COMMAND:
  228.             switch( wParam)
  229.                 {
  230.                 case IDOK:
  231.                     if( ! GetAddressFields( hDlg))
  232.                         break;
  233.                     EndDialog( hDlg, IDOK);
  234.                     break;
  235.  
  236.                 case IDCANCEL:
  237.                     EndDialog( hDlg, IDCANCEL);
  238.                     break;
  239.                 }
  240.             return TRUE;
  241.         }
  242.  
  243.     return FALSE;
  244.     }
  245.  
  246.  
  247. /***************************************************************************
  248.  * Function : SetAddressFields
  249.  *
  250.  * Purpose  : This function initializes the address dialog fields.  It
  251.  *            uses the static 'address' structure.
  252.  *
  253.  * Returns  : n/a
  254.  ***************************************************************************/
  255. static void SetAddressFields( HWND hDlg)
  256.     {
  257.     SetDlgItemText( hDlg, IDC_EDIT_STREET, address.szStreet);
  258.     SetDlgItemText( hDlg, IDC_EDIT_CITY, address.szCity);
  259.     SetDlgItemText( hDlg, IDC_EDIT_STATE, address.szState);
  260.     SetDlgItemText( hDlg, IDC_EDIT_ZIP, address.szZip);
  261.     SetDlgItemText( hDlg, IDC_EDIT_TEL, address.szTelephone);
  262.     SetDlgItemText( hDlg, IDC_EDIT_FAX, address.szFax);
  263.     }
  264.  
  265.  
  266. /***************************************************************************
  267.  * Function : GetAddressFields
  268.  *
  269.  * Purpose  : This function retrieves the address dialog fields.  It
  270.  *            also performs minor field error checking.  Address fields
  271.  *            are stored in the static 'address' structure.
  272.  *
  273.  * Returns  : TRUE  - fields ok
  274.  *            FALSE - error in field (focus is set)
  275.  ***************************************************************************/
  276. static BOOL GetAddressFields( HWND hDlg)
  277.     {
  278.     GetDlgItemText( hDlg, IDC_EDIT_STREET, address.szStreet,
  279.         sizeof( address.szStreet));
  280.     if( ! *address.szStreet)
  281.         {
  282.         MessageBox( hDlg, "Street required", "Invalid Input",
  283.             MB_APPLMODAL | MB_ICONEXCLAMATION | MB_OK);
  284.         SetFocus( GetDlgItem( hDlg, IDC_EDIT_STREET));
  285.         return FALSE;
  286.         }
  287.     GetDlgItemText( hDlg, IDC_EDIT_CITY, address.szCity,
  288.         sizeof( address.szCity));
  289.     if( ! *address.szCity)
  290.         {
  291.         MessageBox( hDlg, "City required", "Invalid Input",
  292.             MB_APPLMODAL | MB_ICONEXCLAMATION | MB_OK);
  293.         SetFocus( GetDlgItem( hDlg, IDC_EDIT_CITY));
  294.         return FALSE;
  295.         }
  296.     GetDlgItemText( hDlg, IDC_EDIT_STATE, address.szState,
  297.         sizeof( address.szState));
  298.     if( ! *address.szState)
  299.         {
  300.         MessageBox( hDlg, "State required", "Invalid Input",
  301.             MB_APPLMODAL | MB_ICONEXCLAMATION | MB_OK);
  302.         SetFocus( GetDlgItem( hDlg, IDC_EDIT_STATE));
  303.         return FALSE;
  304.         }
  305.     GetDlgItemText( hDlg, IDC_EDIT_ZIP, address.szZip,
  306.         sizeof( address.szZip));
  307.     if( ! *address.szZip)
  308.         {
  309.         MessageBox( hDlg, "Zip Code required", "Invalid Input",
  310.             MB_APPLMODAL | MB_ICONEXCLAMATION | MB_OK);
  311.         SetFocus( GetDlgItem( hDlg, IDC_EDIT_ZIP));
  312.         return FALSE;
  313.         }
  314.     GetDlgItemText( hDlg, IDC_EDIT_TEL, address.szTelephone,
  315.         sizeof( address.szTelephone));
  316.     GetDlgItemText( hDlg, IDC_EDIT_FAX, address.szFax,
  317.         sizeof( address.szFax));
  318.  
  319.     return TRUE;
  320.     }
  321.